<template>
  <div class="components-user-manage">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="userMobile"
            :label="$t('distributionMsg.nickName')+':'"
          >
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.nickName')"
            />
          </el-form-item>
          <el-form-item
            prop="userMobile"
            :label="$t('distributionMsg.distriTelPhone')+':'"
          >
            <el-input
              v-model="searchForm.userMobile"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.distriTelPhone')"
            />
          </el-form-item>
          <el-form-item
            prop="parentUserMobile"
            :label="$t('distributionMsg.inviteesPhoneNumber')+':'"
          >
            <el-input
              v-model="searchForm.parentUserMobile"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.inviteesPhoneNumber')"
            />
          </el-form-item>
          <!-- 状态 -->
          <el-form-item
            prop="state"
            :label="$t('distribUserWallet.status') + ':'"
          >
            <el-select
              v-model="searchForm.state"
              :placeholder="$t('distribUserWallet.status')"
              clearable
            >
              <el-option
                :label="$t('distribUserWallet.ban')"
                :value="-1"
              />
              <el-option
                :label="$t('publics.normal')"
                :value="1"
              />
              <el-option
                :label="$t('distribUserWallet.liquidation')"
                :value="2"
              />
            </el-select>
          </el-form-item>
          <!-- 按钮 -->
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="resetForm('searchForm')"
            >
              {{ $t('crud.emptyBtn') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con">
        <el-table
          ref="userBasictTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
          @sort-change="onSortChange"
        >
          <el-table-column
            :label="$t('users.nickName')"
            prop="nickName"
            align="left"
            width="150"
          >
            <template #default="scope">
              {{ scope.row.nickName ? scope.row.nickName : '-' }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distributionMsg.distriTelPhone')"
            prop="userMobile"
            width="200"
          />
          <el-table-column
            :label="$t('distributionMsg.inviteesPhoneNumber')"
            prop="parentUserMobile"
            width="200"
          >
            <template #default="scope">
              <div v-if="scope.row.parentDistributionUser === null">
                -
              </div>
              <div v-else>
                {{ scope.row.parentDistributionUser.userMobile || '-' }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distributionMsg.invitees')"
            prop="parentName"
          >
            <template #default="scope">
              <div v-if="scope.row.parentDistributionUser === null">
                -
              </div>
              <div v-else>
                {{ scope.row.parentDistributionUser.nickName || '-' }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distributionMsg.joiningTime')"
            prop="bindTime"
            sortable
            width="200"
          />
          <el-table-column
            :label="$t('distributionMsg.cumulativeCustomers')"
            prop="boundCustomers"
            sortable
            width="130"
          >
            <template #default="scope">
              {{ scope.row.distributionUserAchievementDataDto.boundCustomers }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distributionMsg.cumulativeInvitation')"
            prop="invitedVeeker"
            sortable
            width="130"
          >
            <template #default="scope">
              {{
                scope.row.distributionUserAchievementDataDto.invitedVeeker
              }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distributionMsg.cumulativeIncome')"
            prop="addupAmount"
            sortable
            width="130"
          >
            <template #default="scope">
              {{
                scope.row.distributionUserAchievementDataDto.distributionUserWallet
                  .addupAmount
              }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('product.status')"
            prop="state"
          >
            <template #default="scope">
              <span v-if="scope.row.state === 0">{{ $t('product.pendingReview') }}</span>
              <span v-else-if="scope.row.state === 1">{{ $t('publics.normal') }}</span>
              <span v-else-if="scope.row.state === -1">{{ $t('distribUserWallet.banned') }}</span>
              <span v-else-if="scope.row.state === 2">{{ $t('distribUserWallet.cleared') }}</span>
              <span v-else-if="scope.row.state === 3">{{ $t('publics.unPassed') }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('crud.menu')"
            width="180"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('distribution:distributionUser:info')"
                  class="default-btn text-btn"
                  @click="onViewInfo(scope.row)"
                >
                  {{ $t("seckill.view") }}
                </div>

                <div
                  v-if="isAuth('distribution:distributionUser:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row)"
                >
                  {{ $t("distributionMsg.updateStatus") }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>

    <!-- 弹窗, 新增 / 修改 -->
    <UserUpdate
      v-if="userUpdateVisible"
      ref="userUpdateRef"
      @refresh-data-list="refreshChange"
    />

    <!-- 弹窗, 新增 / 修改 -->
    <Info
      v-if="infoVisible"
      ref="infoRef"
    />
  </div>
</template>
<script setup>
import UserUpdate from './update.vue'
import Info from './info.vue'
import { isAuth } from '@/utils/index'

let tempSearchForm = null // 保存上次点击查询的请求条件
let theParams = null // 保存上次点击查询的请求条件

const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  userMobile: '',
  parentUserMobile: '',
  state: '',
  nickName: null
})
const dataList = ref([])

const sortParam = ref(0) // 0无 1加入时间 2累计客户 3累计邀请 4累计收益
const sortType = ref(0) // 0无 1 正序 2倒序
onMounted(() => {
  getDataList()
})

// 分销员信息更新
const userUpdateRef = ref(null)
const userUpdateVisible = ref(false)
const onAddOrUpdate = (data) => {
  userUpdateVisible.value = true
  nextTick(() => {
    userUpdateRef.value?.init(data)
  })
}

// 分销员信息
const infoRef = ref(null)
const infoVisible = ref(false)
const onViewInfo = (data) => {
  infoVisible.value = true
  nextTick(() => {
    infoRef.value?.init(data)
  })
}

// 点击查询
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}
// 刷新回调
const refreshChange = () => {
  getDataList(page)
}
// 获取数据列表
const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    theParams = JSON.parse(JSON.stringify(searchForm))
    tempSearchForm = {
      current: pageParam ? pageParam.currentPage : 1,
      size: pageParam ? pageParam.pageSize : 10,
      sortParam: sortParam.value,
      sortType: sortType.value
    }
  } else {
    tempSearchForm.current = page ? page.currentPage : 1
    tempSearchForm.size = page ? page.pageSize : 10
  }
  http({
    url: http.adornUrl('/distribution/distributionUser/page'),
    method: 'get',
    params: http.adornParams(Object.assign(tempSearchForm, theParams))
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
    page.pageSize = data.size
    page.currentPage = data.current
  })
}
const onSortChange = (data) => {
  // 排序字段 0无 1加入时间 2累计客户 3累计邀请 4累计收益
  switch (data.prop) {
    case 'bindTime': sortParam.value = 1
      break
    case 'boundCustomers': sortParam.value = 2
      break
    case 'invitedVeeker': sortParam.value = 3
      break
    case 'addupAmount': sortParam.value = 4
      break
    default: sortParam.value = 0
      break
  }
  // 排序类型 0无 1 正序 2倒序
  switch (data.order) {
    case 'descending': sortType.value = 2
      break
    case 'ascending': sortType.value = 1
      break
    default: sortType.value = 0
      break
  }
  getDataList(page)
}
const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}

const searchFormRef = ref(null)
const resetForm = () => {
  searchFormRef.value.resetFields()
}

</script>
<style scoped>
</style>
